import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '@/views/login.vue')
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('@/views/home.vue'),
    children: [
      {
        path: '/home',
        redirect: '/welcome'
      },
      {
        path: '/welcome',
        component: () => import('@/views/welcome.vue')
      },
      {
        path: '/users',
        component: () => import('@/components/users')
      },
      {
        path: '/roles',
        component: () => import('@/components/roles')
      },
      {
        path: '/rights',
        component: () => import('@/components/rights')
      },
      {
        path: '/goods',
        component: () => import('@/components/goods')
      },
      {
        path: '/params',
        component: () => import('@/components/params')
      },
      {
        path: '/categories',
        component: () => import('@/components/categories')
      },
      {
        path: '/orders',
        component: () => import('@/components/orders')
      },
      {
        path: '/reports',
        component: () => import('@/components/reports.vue')
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// 添加路由守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    return next()
  }
  var token = window.sessionStorage.getItem('token')
  if (!token) {
    return next('/login')
  }
  next()
})

export default router
